import 'package:flutter/material.dart';
import '../models/course.dart';
import '../utils/theme_colors.dart';

class CourseCard extends StatelessWidget {
  final Course course;
  final VoidCallback? onTap;
  final bool isCompact;

  const CourseCard({
    Key? key,
    required this.course,
    this.onTap,
    this.isCompact = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(12),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.05),
              blurRadius: 8,
              offset: const Offset(0, 2),
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildImageSection(),
            _buildContentSection(),
          ],
        ),
      ),
    );
  }

  Widget _buildImageSection() {
    return Container(
      height: isCompact ? 100 : 120,
      decoration: BoxDecoration(
        borderRadius: const BorderRadius.vertical(top: Radius.circular(12)),
        gradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: [
            ThemeColors.primary.withOpacity(0.8),
            ThemeColors.secondary.withOpacity(0.8),
          ],
        ),
      ),
      child: Stack(
        children: [
          // 课程图标
          const Center(
            child: Icon(
              Icons.play_circle_outline,
              size: 40,
              color: Colors.white,
            ),
          ),
          // 状态标签
          Positioned(
            top: 8,
            left: 8,
            child: _buildStatusBadge(),
          ),
          // 难度标签
          Positioned(
            top: 8,
            right: 8,
            child: _buildDifficultyBadge(),
          ),
          // 价格标签
          if (course.price > 0)
            Positioned(
              bottom: 8,
              right: 8,
              child: Container(
                padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Text(
                  '¥${course.price}',
                  style: TextStyle(
                    fontSize: 12,
                    fontWeight: FontWeight.bold,
                    color: ThemeColors.primary,
                  ),
                ),
              ),
            ),
          // 免费标签
          if (course.price == 0)
            Positioned(
              bottom: 8,
              right: 8,
              child: Container(
                padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                decoration: BoxDecoration(
                  color: Colors.green,
                  borderRadius: BorderRadius.circular(12),
                ),
                child: const Text(
                  '免费',
                  style: TextStyle(
                    fontSize: 12,
                    fontWeight: FontWeight.bold,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
        ],
      ),
    );
  }

  Widget _buildStatusBadge() {
    Color backgroundColor;
    String text;
    
    switch (course.status) {
      case CourseStatus.upcoming:
        backgroundColor = Colors.orange;
        text = '即将开课';
        break;
      case CourseStatus.enrolling:
        backgroundColor = Colors.blue;
        text = '报名中';
        break;
      case CourseStatus.ongoing:
        backgroundColor = Colors.green;
        text = '正在进行';
        break;
      case CourseStatus.completed:
        backgroundColor = Colors.grey;
        text = '已结束';
        break;
      case CourseStatus.cancelled:
        backgroundColor = Colors.red;
        text = '已取消';
        break;
    }
    
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 3),
      decoration: BoxDecoration(
        color: backgroundColor,
        borderRadius: BorderRadius.circular(10),
      ),
      child: Text(
        text,
        style: const TextStyle(
          color: Colors.white,
          fontSize: 10,
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }

  Widget _buildDifficultyBadge() {
    Color color;
    String text;
    
    switch (course.difficulty) {
      case CourseDifficulty.beginner:
        color = Colors.green;
        text = '初级';
        break;
      case CourseDifficulty.intermediate:
        color = Colors.orange;
        text = '中级';
        break;
      case CourseDifficulty.advanced:
        color = Colors.red;
        text = '高级';
        break;
      case CourseDifficulty.expert:
        color = Colors.purple;
        text = '专家级';
        break;
    }
    
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 3),
      decoration: BoxDecoration(
        color: color.withOpacity(0.9),
        borderRadius: BorderRadius.circular(10),
      ),
      child: Text(
        text,
        style: const TextStyle(
          color: Colors.white,
          fontSize: 10,
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }

  Widget _buildContentSection() {
    return Padding(
      padding: const EdgeInsets.all(12),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 课程标题
          Text(
            course.title,
            style: const TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.bold,
            ),
            maxLines: 2,
            overflow: TextOverflow.ellipsis,
          ),
          const SizedBox(height: 8),
          
          // 课程描述
          if (!isCompact)
            Text(
              course.description,
              style: TextStyle(
                fontSize: 13,
                color: ThemeColors.textSecondary,
                height: 1.4,
              ),
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
            ),
          if (!isCompact) const SizedBox(height: 12),
          
          // 讲师信息
          Row(
            children: [
              CircleAvatar(
                radius: 12,
                backgroundColor: ThemeColors.primary.withOpacity(0.1),
                child: Text(
                  course.instructor.name.substring(0, 1),
                  style: TextStyle(
                    fontSize: 10,
                    fontWeight: FontWeight.bold,
                    color: ThemeColors.primary,
                  ),
                ),
              ),
              const SizedBox(width: 8),
              Expanded(
                child: Text(
                  course.instructor.name,
                  style: const TextStyle(
                    fontSize: 12,
                    fontWeight: FontWeight.w500,
                  ),
                  overflow: TextOverflow.ellipsis,
                ),
              ),
              // 评分
              Icon(
                Icons.star,
                size: 14,
                color: Colors.amber,
              ),
              const SizedBox(width: 2),
              Text(
                course.instructor.rating.toString(),
                style: const TextStyle(
                  fontSize: 11,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ],
          ),
          const SizedBox(height: 8),
          
          // 课程统计信息
          Row(
            children: [
              // 学员数量
              Icon(
                Icons.people,
                size: 14,
                color: ThemeColors.textSecondary,
              ),
              const SizedBox(width: 4),
              Text(
                '${course.enrolledCount}人',
                style: TextStyle(
                  fontSize: 11,
                  color: ThemeColors.textSecondary,
                ),
              ),
              const SizedBox(width: 12),
              
              // 课程时长
              Icon(
                Icons.access_time,
                size: 14,
                color: ThemeColors.textSecondary,
              ),
              const SizedBox(width: 4),
              Text(
                '${course.duration}小时',
                style: TextStyle(
                  fontSize: 11,
                  color: ThemeColors.textSecondary,
                ),
              ),
              const SizedBox(width: 12),
              
              // 章节数量
              Icon(
                Icons.video_library,
                size: 14,
                color: ThemeColors.textSecondary,
              ),
              const SizedBox(width: 4),
              Text(
                '${course.chapters.length}章',
                style: TextStyle(
                  fontSize: 11,
                  color: ThemeColors.textSecondary,
                ),
              ),
              
              const Spacer(),
              
              // 课程类型
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                decoration: BoxDecoration(
                  color: ThemeColors.primary.withOpacity(0.1),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Text(
                  course.type.displayName,
                  style: TextStyle(
                    fontSize: 10,
                    color: ThemeColors.primary,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
            ],
          ),
          
          // 进度条（如果用户已报名）
          if (course.isEnrolled) ...[
            const SizedBox(height: 8),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      '学习进度',
                      style: TextStyle(
                        fontSize: 11,
                        color: ThemeColors.textSecondary,
                      ),
                    ),
                    Text(
                      '${(course.progress * 100).toInt()}%',
                      style: TextStyle(
                        fontSize: 11,
                        color: ThemeColors.primary,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 4),
                LinearProgressIndicator(
                  value: course.progress,
                  backgroundColor: ThemeColors.primary.withOpacity(0.1),
                  valueColor: AlwaysStoppedAnimation<Color>(ThemeColors.primary),
                  minHeight: 3,
                ),
              ],
            ),
          ],
        ],
      ),
    );
  }
}